package ysq.xyg.sale.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sale.dao.SaleColumnDao;
import ysq.xyg.sale.model.po.SaleColumn;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sale.model.vo.SaleColumnVo;
import ysq.xyg.sale.service.SaleColumnService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 销售模块-分类栏目service实现
 *
 * @author shengQiang yu
 * @date 2019-9-9 13:56:53
 */
@Service("saleColumnService")
public class SaleColumnServiceImpl extends BaseServiceImpl<SaleColumn> implements SaleColumnService {

    @Resource
    private SaleColumnDao saleColumnDao;

    private SaleColumn saleColumn;

    @Autowired
    public void setBaseDao(SaleColumnDao saleColumnDao) {
        this.baseDao = saleColumnDao;
    }


    /**
     * 分页查询
     *
     * @param saleColumnVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 13:56:53
     */
    public ReturnEntity page(SaleColumnVo saleColumnVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SaleColumn> list = acceptList(saleColumnVo);
        PageInfo<SaleColumn> page=new PageInfo<SaleColumn>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param saleColumnVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 13:56:53
     */
    public ReturnEntity addOrUpdate(SaleColumnVo saleColumnVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(saleColumnVo.getId())){
            saleColumn=new SaleColumn(currentUser);
            isUpate=false;
        }else{
            saleColumn=selectByPrimaryKey(saleColumnVo.getId());
            if(ObjectUtil.isNull(saleColumn)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            saleColumn.setUpdateTime(new Date());
            saleColumn.setUpdateUser(currentUser.getId());
        }
        saleColumn.setName(saleColumnVo.getName());
        saleColumn.setDescription(saleColumnVo.getDescription());
        saleColumn.setSort(saleColumnVo.getSort());
        saleColumn.setShowType(saleColumnVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(saleColumn);
        }else{
            num=insert(saleColumn);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 13:56:53
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SaleColumn saleColumn=selectByPrimaryKey(id);
        if(saleColumn!=null){
            saleColumn.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            saleColumn.setUpdateTime(new Date());
            saleColumn.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(saleColumn);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    @Override
    public ReturnEntity acceptSelect(String code) {
        List<SaleColumn> list = acceptList(new SaleColumnVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(SaleColumn webColumnType:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(webColumnType.getId());
            sysDictContent.setName(webColumnType.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(webColumnType.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
    /**
     * 获取集合
     *
     * @param saleColumnVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-9-9 13:56:53
     */
    public List<SaleColumn> acceptList(SaleColumnVo saleColumnVo){
        Example example = new Example(SaleColumn.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(saleColumnVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",saleColumnVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return saleColumnDao.selectByExample(example);
    }
}
